package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import po.Result;
import po.Tenant;
import service.TenantService;

import java.util.List;

@Controller
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    private TenantService tenantService;

    @RequestMapping("/findAllTenants")
    public @ResponseBody List<Tenant> findAllTenants(Tenant tenant) throws Exception {
        List<Tenant> list = tenantService.findAllTenants(tenant);
        return list;
    }

    @RequestMapping("/getTenantInfoView")
    //返回租户管理界面视图
    public String getTenantInfoView() {
        return "tenant/tenantInfo";
    }

    @RequestMapping("/getAddTenantView")
    //返回添加租户界面视图
    public String getAddTenantView() {
        return "tenant/addTenant";
    }

    @RequestMapping("/getEditTenantView")
    //返回修改租户界面视图
    public ModelAndView getEditTenantView(Tenant tenant) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tenant", tenant);
        modelAndView.setViewName("tenant/editTenant");
        return modelAndView;
    }

    @RequestMapping("/addTenant")
    public @ResponseBody Result addTenant(Tenant tenant) {
        return tenantService.addTenant(tenant);
    }

    @RequestMapping("/deleteTenant")
    public @ResponseBody Result deleteTenant(int id) {
        return tenantService.deleteTenantById(id);
    }

    @RequestMapping("/updateTenant")
    public @ResponseBody Result updateTenant(Tenant tenant) {
        return tenantService.updateTenantById(tenant.getId(), tenant);
    }
}
